package com.bwie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.mapper.ProdImageMapper;
import com.bwie.pojo.Prod;
import com.bwie.pojo.ProdImage;
import com.bwie.service.ProdService;
import com.bwie.mapper.ProdMapper;
import com.bwie.utils.Result;
import com.bwie.vo.AttaVo;
import com.bwie.vo.ProdPageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author 陶仕豪
 * @description 针对表【tb_prod(商品表)】的数据库操作Service实现
 * @createDate 2025-05-26 20:59:18
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class ProdServiceImpl extends ServiceImpl<ProdMapper, Prod>
        implements ProdService {
    @Autowired
    ProdMapper prodMapper;
    @Autowired
    ProdImageMapper prodImageMapper;

    @Override
    public Result getProdList(ProdPageVo prodPageVo) {
        Page<Prod> prodPage = new Page<>(prodPageVo.getPageNum(), prodPageVo.getPageSize());
//        QueryWrapper<Prod> wrapper = new QueryWrapper<>();
//        wrapper.lambda().orderByDesc(Prod::getProdId);
//        Page<Prod> page = prodMapper.selectPage(prodPage, wrapper);
        Page<Prod> page = prodMapper.selectProdList(prodPage, prodPageVo);
        return Result.ok().setData(page);
    }

    @Override
    public Result addProd(Prod tbProd) {
        //SPU重复校验
        Prod prod = prodMapper.selectOne(new QueryWrapper<Prod>().lambda().eq(Prod::getProdSpu, tbProd.getProdSpu()));
        if (prod != null) {
            return Result.error().setMsg("商品SPU已存在");
        }
        //名称重复校验
        prod = prodMapper.selectOne(new QueryWrapper<Prod>().lambda().eq(Prod::getProdName, tbProd.getProdName()));
        if (prod != null) {
            return Result.error().setMsg("商品S名称已存在");
        }
        tbProd.setProdStatus(0);
        tbProd.setCreateBy("admin123");

        prodMapper.insert(tbProd);
        return Result.ok();
    }

    @Override
    public Result deleteAllProd(List<Prod> prodList) {
        for (Prod prod : prodList) {
            prodMapper.deleteById(prod);
        }
        return Result.ok();
    }

    @Override
    public Result addProdAtta(AttaVo attaVo) {
        //确定商品是否存在
        Prod prod = prodMapper.selectById(attaVo.getProdId());
        if (prod == null) {
            return Result.error().setMsg("商品不存在");
        }

        //更新商品信息
        prod.setProdVideo(attaVo.getProdVideo());
        prod.setUpType(attaVo.getUpType());
        prod.setUpTime(attaVo.getUpTime());
        prod.setDownType(attaVo.getDownType());
        prod.setDownTime(attaVo.getDownTime());
        prodMapper.updateById(prod);

        //删除之前的图片
        prodImageMapper.delete(new QueryWrapper<ProdImage>().lambda().eq(ProdImage::getProdId, attaVo.getProdId()));
        //添加商品附件信息
        if (attaVo.getImageList() != null && attaVo.getImageList().size() > 0) {
            for (String image : attaVo.getImageList()) {
                ProdImage prodImage = new ProdImage();
                prodImage.setProdId(prod.getProdId());
                prodImage.setImageUrl(image);
                prodImageMapper.insert(prodImage);
            }
        }
        return Result.ok();
    }
}




